I have a loop showing post titles and excerpts:
<?php query_posts('category_name=blog&showposts=6'); while (have_posts()) : the_post(); ?>
<div class="panel">
<h2><?php the_title(); ?></h2>
<div class="excerpt"><?php the_excerpt(); ?></div>
<a class="readmore" href="<?php the_permalink(); ?>">Continue Reading</a>
</div>
<?php endwhile; wp_reset_query(); ?>
I want to be able to show how many views each post has had in this loop as well.
I am aware there are many ways to show this on the post page (single.php), but I cant find any to show it in a loop. Plugins only seem to work on the single.php page as well.
Does anyone know a way to achieve this?
You will need to find out where the view count are getting stored for each post by the plugin. Generally it gets stored in post meta.
If this is the case, Then you need to use the following code to display the view count.
<?php query_posts('category_name=blog&showposts=6'); while (have_posts()) : the_post(); ?>
<div class="panel">
<h2><?php the_title(); ?></h2>
Views for this post: <?php echo get_post_meta(get_the_ID(),'meta_name_view_count',true); ?>
<div class="excerpt"><?php the_excerpt(); ?></div>
<a class="readmore" href="<?php the_permalink(); ?>">Continue Reading</a>
</div>
<?php endwhile; wp_reset_query(); ?>
meta_name_view_count this will be the name what plugin uses to store the view count in wp_postmeta table
Related
I have a repeater in my footer.php file and it works for the entire website, but whenever I go to the blog page (index.php), the repeater only displays one row of the entire repeater and I don't know why this is happening.
I've tried putting get_option('page_for_posts') as the repeater field's second parameter, but this didn't work.
This is how it looks on every page except the blog page.
and this is how it looks on the blog page itself:
It only shows one row on the blog page, which is the About Us column.
Here's the code:
<?php if ( have_rows('post_object_repeater') ) : ?>
<?php while( have_rows('post_object_repeater') ) : the_row(); ?>
<?php $header = get_sub_field( 'header' ); ?>
<div class="<?php echo $number_of_columns; ?> col-md-6 col-12 list-column">
<?php
$posts = get_sub_field('post_object_relationship');
if( $posts ): ?>
<ul class="list-unstyled <?php if ( ! $header ): echo 'no-header'; endif; ?>">
<?php if ( $header ): ?>
<li class="header mb-3 font-weight-bold text-uppercase"><?php echo $header; ?></li>
<?php endif; ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<li class="list-item">
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
This repeater does have a relationship field inside to pull each of the individual pages or custom post types of the website. Would anyone have an idea why not all of the repeater rows display on a blog page, but work on all other pages?
Have you tried
if(have_rows('post_object_repeater', 'option')
while(have_rows('post_object_repeater', 'option'))
etc.
????
I've created a custom post using advanced custom fields on my wordpress theme. Its a profile page that displays a users basic info plus a gallery section titled weekly activity and an archive section. I would like to archive the gallery displayed under "weekly activity" so that the archives are listed on the page under "archives". I'm not quite sure how to tackle that particular are. Here is my code for the front-end template:
<?php get_header(); ?>
<?php the_post(); ?>
<div class = "hcontainer">
<div class = "leftcol">
<?php
$image = get_field('profile_picture');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<p class ="lfeilds">Name:<?php the_field('name'); ?></p>
<p class ="lfeilds">Age:<?php the_field('age'); ?></p>
<p class ="lfeilds">Gender:<?php the_field('gender'); ?></p>
<p class ="larchive">Gallery Archive</p>
***<?php the_field('archive'); ?>***
</div>
<div class ="rightcol">
<p class ="rtitle">Weekly Activities</p>
<div class ="rgallery">
<?php
$image_ids = get_field('gallery', false, false);
$shortcode = '[gallery ids="' . implode(',', $image_ids) . '"]';
echo do_shortcode( $shortcode );
?>
</div>
</div>
</div>
In magento backend, I though no matter homepage or other pages should use the same header file. However, when I change the logo picture for header in backend, only homepage's header picture was changed successfully, other pages are not showing the picture correctly.
Basically what I did is, there are two rows in header, so I put half of the logo on left of the first row and and the other half of the logo on left of the second row, so it seems like a bigger logo.
homepage pic
what it should be
catalog and product view page
in catalog and product view page(bottom part of the logo is not changed)
in secondary catalog page, the upper part of the logo is missing, and bottom part of the logo is not changed.
code path: design/frontend/theme/template/page/html/header.phtml
<div class="header-top">
<img src="media/images/logoUp.jpg" style="width:500px;height:60px;">
<div class="header-top-search-wrapper">
<?php echo $this->getChildHtml('topSearch') ?>
<?php /*TODO: ?><a class="advanced-search-link" href="<?php echo Mage::getURL('') ?>catalogsearch/advanced/"><?php print $this->__('Advanced Search') ?></a><?php */?>
</div>
<?php echo $this->getChildHtml('currency') ?>
<?php echo $this->getChildHtml('store_language') ?>
<?php if ($tmp_html = $this->getChildHtml('i_block_header_top_help')): ?> <?php //Predefined CMS block ?>
<div class="header-top-help-wrapper"><?php echo $tmp_html; ?></div>
<?php endif; ?>
<?php if ($tmp_html = $this->getChildHtml('i_block_header_top_links')): ?> <?php //Predefined CMS block ?>
<div class="header-top-links-wrapper"><?php echo $tmp_html; ?></div>
<?php endif; ?>
</div>
//<?php if ($this->getIsHomePage()):?>
<h1 class="logo"><strong><?php echo $this->getLogoAlt() ?></strong><img src="media/images/logoDown.jpg" style="width:500px;height:60px;"></h1>
//<?php else:?>
// <strong><?php echo $this->getLogoAlt() ?></strong><img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt() ?>" />
// <?php endif?>
<div class="header-left">
<?php if ($tmp_html = $this->getChildHtml('i_block_header_contact')): ?> <?php //Predefined CMS block ?>
<div class="header-contact-wrapper"><?php echo $tmp_html; ?></div>
<?php endif; ?>
<?php if ($tmp_html = $this->getChildHtml('i_block_header_banner')): ?> <?php //Predefined CMS block ?>
<div class="header-banner-wrapper"><?php echo $tmp_html; ?></div>
<?php endif; ?>
</div> <!-- end: header-left -->
<div class="header-right">
<div class="header-right-secondary">
<p class="welcome-msg"><?php echo $this->getWelcome() ?> <?php echo $this->getAdditionalHtml() ?></p>
<?php echo $this->getChildHtml('topContainer'); ?>
</div>
<div class="user-menu">
<?php // Show custom link, if enabled ?>
<?php if ($themeHelper->getCfg('header/links_custom_link_enabled')): ?>
<?php
$_customLinkPath = $themeHelper->getCfg('header/links_custom_link_path');
$_customLinkAnchor = $themeHelper->getCfg('header/links_custom_link_anchor');
$_customLinkTitle = $themeHelper->getCfg('header/links_custom_link_title');
?>
<ul>
<li class="user-menu-item user-menu-custom">
<?php echo $_customLinkAnchor; ?>
</li>
</ul>
<?php endif?>
<?php echo $this->getChildHtml('topLinks') ?>
<?php // If customer is not logged in: show "Sign Up" link, if enabled ?>
<?php if (Mage::getSingleton('customer/session')->isLoggedIn() == false): ?>
<?php if ($themeHelper->getCfg('header/links_signup_enabled')): ?>
<ul>
<li class="user-menu-item user-menu-signup">
<?php echo $this->__('Sign Up') ?>
</li>
</ul>
<?php endif?>
<?php endif?>
<ul>
<li id="mini-cart" class="user-menu-item user-menu-cart">
<?php echo $this->getChildHtml('cart_sidebar') ?>
</li>
</ul>
</div>
</div> <!-- end: header-right -->
The above code is for header's part.
I'm quite confused, shouldn't all pages use the same header file? why they appear differently?
Thank you
I know the problem now.
The reason why secondary categories can't get the image is because the url is hostname/categoryName/image.jpg
So the url is not valid here anymore. Should have use dynamic url here.
my code is
<?php
// Template Name: homepage
get_header(); ?>
<div id="content" class="full-width">
<?php
query_posts(array(
'post_type' => 'avada_portfolio',
'showposts' => 2
) );
?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<img src=" <?php the_post_thumbnail('medium'); ?>">
<p><?php echo get_the_excerpt(); ?></p>
<?php
endwhile; //resetting the page loop
wp_reset_query(); //resetting the page query
?>
<h1 class="entry-title"><?php the_title(); ?></h1> <!-- Page Title -->
<?php
// TO SHOW THE PAGE CONTENTS
while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
<div class="entry-content-page">
<?php the_content(); ?> <!-- Page Content -->
</div><!-- .entry-content-page -->
<?php
endwhile; //resetting the page loop
wp_reset_query(); //resetting the page query
?>
<?php get_footer(); ?>
I have added the <?php add_theme_support( 'post-thumbnails' ); ?> to functions however it just shows a broken image on my home page :(
All my images are uploaded onto my wordpress site and set as featured images on the custom post!
thanks
the_post_thumbnail('medium') this function will return img tag with feature image.
So you can get image following two ways:-
<img src=" <?php echo wp_get_attachment_url(get_post_thumbnail_id( $post->ID ), 'medium' ); ?>">
OR just this function :- <?php the_post_thumbnail('medium'); ?>
Hope this will help you.
<img src=" <?php the_post_thumbnail('medium'); ?>">
This does not work in <img src="">. For me this worked:
<img src=" <?php echo wp_get_attachment_url(get_post_thumbnail_id( get_the_ID() ), 'thumbnail' ); ?>" class="media-object" style="width:73px; height:73px">
write "the_post_thumbnail('medium');" out of the image tag
Like this:
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail('medium'); ?>
<p><?php echo get_the_excerpt(); ?></p>
<?php
endwhile; //resetting the page loop
wp_reset_query(); //resetting the page query
?>
I am rather new to Wordpress and have a question. I have created my own theme, which all seems to work fine. But, I am having one issue. I want to create my blog page (with all the posts) on a page other than my home page. So, in my theme folder, I created a page template called blog.php:
<?php
/*
Template Name: blog
*/
?>
<?php get_header(); ?>
<table id="about-table" >
<tr>
<td colspan="7">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php the_title(); ?>
<?php the_author(); ?>
<?php the_time("jS F"); ?>
<?php comments_number("0","1","%"); ?>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
</td>
</tr>
</table>
<?php get_footer(); ?>
Then, I created a page in wordpress called "blog" as well, in the "pages" section in the dashboard. I then assigned its template to the above "blog" template. The problem is, though, that the code does not work as it should. Instead of showing me the titles, comments, etc of the posts, it displays some other info. On the other hand, if i just copy this:
<table id="about-table" >
<tr>
<td colspan="7">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php the_title(); ?>
<?php the_author(); ?>
<?php the_time("jS F"); ?>
<?php comments_number("0","1","%"); ?>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
</td>
</tr>
</table>
to my index page, it works fine. So, how do I display all my post info on a page other than the home page?
I wanted to provide you with a simpler loop as a second option. If you use this and set Reading settings to the specific blog page this works well:
<?
/*
Template Name: Blog Template Example
*/
?>
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="next-posts"><?php next_posts_link(); ?></div>
<div class="prev-posts"><?php previous_posts_link(); ?></div>
</div>
<?php else : ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1>Not Found</h1>
</div>
<?php endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
How about you do something like this:
blog-template.php:
<?php/*
Template Name: Blog Page
*/
?>
<?php get_header(); ?>
<?php get_template_part( 'layout-page', 'blog' );?>
<?php get_footer(); ?>
layout-page-blog.php:
<?php
the_post();
$title = get_the_title();
$baselink = get_permalink();
$category = get_field('category_blog');
if( !empty($category) ){
$post_per_page = get_option('posts_per_page');
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$categoryID = get_category_id($category);
$total = get_post_count(array($categoryID));
$the_query = new WP_Query("posts_per_page={$post_per_page}&cat= {$categoryID}&paged={$paged}");
?>
<div id="wrapper">
<div id="content">
<h1 class="title"><?php echo $title; ?></h1>
<div class="content-middle">
<div class="node">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<div class="content">
<?php echo content(150); ?>
</div>
<div class="read-more">Read more</div>
<?php endwhile; ?>
<br/><br/>
<div class="wp-paginate">
<?php
wp_reset_query();
echo paginate_links( array(
'base' => $baselink.'%_%',
'total' => ceil($total/$post_per_page),
'current' => $paged,
));
?>
</div>
</div>
</div>
</div> <!-- end content -->
<div style="clear:both"></div>
</div>
<?php
}
?>
This could all be in one file, or you can use it in two pieces as I have written it.
EDIT:
Sorry I have it set to also grab the images from the post. I think this is the functions code you need:
function get_images_by_cat($id){
$limit = 1000;
$the_query = new WP_Query("posts_per_page={$limit}&cat={$id}");
$arr = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title = get_the_title();
$image_src = get_field('banner_image');
$image_link = get_field('banner_link');
$arr[] = array(
"title" => $title,
"link" => $image_link,
"image" => $image_src,
);
}
wp_reset_query();
return $arr;
}