grid layout default on wordpress theme

2019-09-01 03:28发布

问题:

I'm having trouble with a multi-layout option on a wordpress theme sight http://sight.wpshower.com/

the traffic have the option of a grid or a list layout at the click of a button. at present the list layout is default. I am interested in making the grid layout default .

this is some of the php, i tried simply swapping the word grid for list but although this does work to an extent , if done on the loop.php page it removes the a:hover functions on the post boxes in the grid format. also if done on the index.php it switches buttons on the main index page.

any ideas??

loop.php

   <div id="loop" class="<?php if ($_COOKIE['mode'] == 'grid') echo 'grid'; else     echo 'list'; ?> clear"> 

     <?php while ( have_posts() ) : the_post(); ?>

        <div <?php post_class('post clear'); ?> id="post_<?php the_ID(); ?>">
        <?php if ( has_post_thumbnail() ) :?>
        <a href="<?php the_permalink() ?>" class="thumb"><?php the_post_thumbnail('thumbnail', array(
                    'alt'   => trim(strip_tags( $post->post_title )),
                    'title' => trim(strip_tags( $post->post_title )),
                )); ?></a>
        <?php endif; ?> 

        <div class="post-category"><?php the_category(' / '); ?></div>
        <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

     <!--   <div class="post-meta">by <span class="post-author"><a
                href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" title="Posts by <?php the_author(); ?>"><?php the_author(); ?></a></span>
                               on <span
                    class="post-date"><?php the_time(__('M j, Y')) ?></span> <em>&bull; </em><?php comments_popup_link(__('No Comments'), __('1 Comment'), __('% Comments'), '', __('Comments Closed')); ?> <?php edit_post_link( __( 'Edit entry'), '<em>&bull; </em>'); ?>  
        </div>  -->  

<?php edit_post_link( __( 'Edit entry'), '<em>&bull; </em>'); ?>

 <div class="post-content"><?php if (function_exists('smart_excerpt')) smart_excerpt(get_the_excerpt(), 55); ?></div>
    </div>

  <?php endwhile; ?>

   </div>

        <?php endif; ?>

index.php

<?php get_header(); ?>

<div class="content-title">
Projects
<a href="javascript: void(0);" id="mode"<?php if ($_COOKIE['mode'] == 'grid') echo ' class="flip"'; ?>></a>
</div> 

 <?php query_posts(array(
    'post__not_in' => $exl_posts,
    'paged' => $paged,
)
 ); ?>

 <?php get_template_part('loop'); ?>

<?php wp_reset_query(); ?>

<?php get_template_part('pagination'); ?>

 <?php get_footer(); ?>

回答1:

So unfortunately I can't comment my previous answer. And yes I know that this topic is really old, but there is a solution to this. Since several files needed to be changed the full answer can be found here: http://alpipego.com/get-the-grid-view-by-default-sight-1-0-1-wpshower-theme-part-iii/



回答2:

I know this is not the newest question. But I guess more people will be searching for an answer. So I'll give it a try.
So I guess we are having multiple problems:

  1. You disabled the hover function because you put too much code inside the comment brackets. I guess you only wanted to remove the author?! This should do it:

    <div class="post-meta"><!-- by <span class="post-author"><a
                href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" title="Posts by <?php the_author(); ?>"><?php the_author(); ?></a></span> -->
    
  2. Modify index.php, loop.php and archive.php.
  3. Then you'll also have to modify the 'js/script.js' as well.
  4. The last step is to change the 'images/mode.png'.

So here is what it looks like:

index.php Line 5:

<a href="javascript: void(0);" id="mode"<?php if ($_COOKIE['mode'] == 'list') echo ' class="flip"'; ?>></a>

loop.php line 3:

<div id="loop" class="<?php if ($_COOKIE['mode'] == 'list') echo 'list'; else echo 'grid'; ?> clear">

archive.php line 22:

    <a href="javascript: void(0);" id="mode"<?php if ($_COOKIE['mode'] == 'list') echo ' class="flip"'; ?>></a>

script.js lines 45-63:

        /*** View mode ***/

    if ( $.cookie('mode') == 'grid' ) {
        grid_update();
    } else if ( $.cookie('mode') == 'list' ) {
        list_update();
    }

    $('#mode').toggle(
        function(){
            if ( $.cookie('mode') == 'list' ) {
                $.cookie('mode','grid');
                grid();
            } else {
                $.cookie('mode','list');
                list();
            }
        },
        function(){
            if ( $.cookie('mode') == 'grid') {
                $.cookie('mode','list');
                list();
            } else {
                $.cookie('mode','grid');
                grid();
            }
        }
    );

    function grid(){
        $('#mode').removeClass('flip');
        $('#loop')
            .fadeOut('fast', function(){
                grid_update();
                $(this).fadeIn('fast');
            })
        ;
    }

    function list(){
        $('#mode').addClass('flip');
        $('#loop')
            .fadeOut('fast', function(){
                list_update();
                $(this).fadeIn('fast');
            })
        ;
    }

    function grid_update(){
        $('#loop').addClass('grid').removeClass('list');
        $('#loop').find('.thumb img').attr({'width': '190', 'height': '190'});
        $('#loop').find('.post')
            .mouseenter(function(){
                $(this)
                    .css('background-color','#DADADA')
                    .find('.thumb').hide()
                    .css('z-index','-1');
            })
            .mouseleave(function(){
                $(this)
                    .css('background-color','#f5f5f5')
                    .find('.thumb').show()
                    .css('z-index','1');
            });
        $('#loop').find('.post').click(function(){
            location.href=$(this).find('h2 a').attr('href');
        });
        $.cookie('mode','grid');
    }

    function list_update(){
        $('#loop').addClass('list').removeClass('grid');
        $('#loop').find('.post').removeAttr('style').unbind('mouseenter').unbind('mouseleave');
        $('#loop').find('.thumb img').attr({'width': '290', 'height': '290'});
        $.cookie('mode', 'list');
    }

Finally you will have to rotate the mode.png 180°

Works for me: Check it out on my site: grid.alpipego.



标签: php wordpress