I want my latest post to be on top of other posts, how can i accomplish this? Now it just posts the latest at the bottom of the one i posted before.
I use this simple code to display my posts:
<div class="container">
<?php foreach ($articles as $article) { ?>
<div class="box">
<div class="boximg"> </div>
<div class="z link">
<a href="article.php?id=<?php echo $article['article_id'] ; ?>">
<?php echo $article['article_title']; ?>
</a>-
<small> posted
<?php echo date('l jS', $article['article_timestamp']); ?>
</small>
</div>
</div>
<?php } ?>
<br>
</div>
Can someone help me with this please?
Try fetching the articles like this:
SELECT *
FROM articles
ORDER BY article_timestamp DESC;
See MySQL ORDER BY: Sort a Result Set for insight.
Related
Can anyone guide me how can i add social share buttons in Magento 2 ?
In success.phtml i have discounted code on that . I want to just send that on social share.
Below is the file that want create. If any module or custom code then help me anyone?
SHare your code i have already done but i am getting issue how to add below sharing option using custom coding.
Below is the success.phtml file
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<?php /** #var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
<div class="main-inside-checkout">
<?php if ($block->getOrderId()) :?>
<div id="main-checkout-first">
<?php if ($block->getCanViewOrder()) :?>
<h1 class="order_status">Your order is Confirmed!</h1>
<img src="<?php echo $block->getUrl("pub/media/icon/")?>order-imge.jpg"/>
<p class="order-number"><?= $block->escapeHtml(__('Your order number is: %1.', sprintf('<strong>%s</strong>', $block->escapeUrl($block->getViewOrderUrl()), $block->getOrderId())), ['a', 'strong']) ?></p>
<?php else :?>
<p><?= $block->escapeHtml(__('Your order # is: <span>%1</span>.', $block->getOrderId()), ['span']) ?></p>
<?php endif;?>
<h5><?= $block->escapeHtml(__('Get 25% Off your next order')) ?></h5>
<p class="order-disc-details">When your friends use your referral code to place their first order,you'll both get <span class="off-percentage">25% Off</span>. It's a win win 🙂</p>
</div>
<div id="main-checkout-second">
<p class="main-input-checkout"><input type="text" value="LGL174" id="discounted-code"><button class="button-for-copy" onclick="copyToClipBoard()">COPY</button></p>
</div>
<div id="main-checkout-second">
Share via Whatsapp
</div>
<?php endif;?>
<?= $block->getAdditionalInfoHtml() ?>
<div class="actions-toolbar">
<div class="primary">
<a class="action primary continue" href="<?= $block->escapeUrl($block->getContinueUrl()) ?>"><span><?= $block->escapeHtml(__('Continue Shopping')) ?></span></a>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function copyToClipBoard() {
var content = document.getElementById('discounted-code');
content.select();
document.execCommand('copy');
}
</script>
If anyone have idea then please let me know
i have a single page website built on wordpress (twenty-thirteen-child template).
header content footer.
and 3 posts categories :category A category B category C.
i need to split the content into these 3 categories/sections -like this
<div class="section_top">
all the posts from category A
</div>
<div class="section_mid">
all the posts from category B
</div>
<div class="section_bot">
all the posts from category C
</div>
i got a little confused when i started to read about the wordpress main loop ,query_posts and WP_Query,eventually i have this code replacing the main loop ,so my questions are :
1 is this the correct way to do it?
2 how to give each section a unique class since i need to style each section diffrently?
here is the code (index php(in child theme)
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$args = array('category__in' => array(catA,catB,catC));
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div id="<?php
foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>"
class="post-item">
<div class="post-info">
<h2 class="post-title">
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
</div>
<div class="post-content">
<?php the_content(); ?>
</div>
</div>
<?php
endwhile;
else:
?>
Oops, there are no posts.
<?php
endif;
?>
The problem with how you're trying to solve it is that all posts are still shown chronologically, so this could happen:
Category A post 12/01
Category B post 09/01
Category A post 05/01
What I'd recommend: make 3 different WordPress loops querying each category separately like so:
<div class="section_top">
WordPress loop for all the posts from category A
</div>
<div class="section_mid">
WordPress loop for all the posts from category B
</div>
<div class="section_bot">
WordPress loop for all the posts from category C
</div>
Example of such a loop:
// The Query
$the_query = new WP_Query('category_name=categoryA');
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Difference between get_the_...() and the_...()
When you use functions like get_the_title() you are able to store them in a PHP variable like so:
$title = get_the_title();
echo strtoupper($title); //do something with it later
When you use functions like the_title() then the title is immediately printed on the page, so there's no need for an echo statement:
the_title();
Notice: the_permalink() has a function get_permalink(), the function get_the_permalink() does not exist. Don't ask my why ;)
How can I display my most viewed posts on a single page ?
For this moment, i've create a new template page (content-most-viewed.php) wich is called by get_template_part();
On Internet, i've read that query_posts could solve my problem, but it seems to fail.
This is my template :
<?php query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
//query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC');?>
<div class="post">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="heading">
<h2><?php the_title();?></h2>
</div>
<?php the_content();?>
<div class="link-holder">Retour</div>
<?php endwhile; ?>
<?php else : ?>
<div class="heading">
<h2>Not Found</h2>
</div>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
But, I can only get recent posts, or "Not Found" (any resutls)
In advance, thanks !
WordPress by itself does not keep track of the number a post is viewed there are pluging that do this: WPP, plugin repos
These plugins will have build in functions to do this.
side note
don't use query_posts its bad for preformance. use WP_query in stead. reasons why it is bad: https://wordpress.stackexchange.com/a/50762/10911, https://wordpress.stackexchange.com/a/50762/10911
I've coded this Wordpress site but I'm finding difficulties with the blog area. Only one blog shows in the blog section but with I am logged in as an admin, I can see all the blogs on the page. I need it to show for everyone, whether they are logged in as a user or just a visitor to the website.
I have attempted to see if it was a wordpress issue by checking the 'Settings >> Reading' settings and they are set just fine, showing to be 10 posts per page.. It could be something wrong with the loop. I have the blog pulling from the index.php.
http://www.ilovepennycakes.com/category/blog/
Here is the direct link to the blog not showing in the feed.
http://www.ilovepennycakes.com/thanksgiving-thoughts/
The code is as follows:
<?php get_header(); ?>
<!-- Article Loop -->
<article>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="news-top"></div>
<div class="news">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-header">
<h1 class="meander"><?php the_title(); ?></h1>
<p class="likes m500"><?php comments_popup_link( '0', '1', '%' ); ?></p>
<div class="clear"></div>
</div><!--end post header-->
<!--div class="entry clear"-->
<div class="blog-content m500">
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
</div>
<!--/div--><!--end entry-->
<p class="date M500">Posted <?php the_time( 'j M Y' ); ?></p>
<p class="M500"><?php edit_post_link( __( 'Edit', 'pennycakes' ), '<span>', '</span>' ); ?></p>
<!--end post footer-->
</div><!--end post-->
</div>
<div class="news-bottom"></div>
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
</div><!--end navigation-->
<div class="navigation index">
<div class="alignleft"><?php next_posts_link( 'Older Entries' ); ?></div>
<div class="alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
<?php else : ?>
<?php endif; ?>
</article>
<!-- //Article Loop -->
Any help will be appreciated.
Hmmm... are your posts still "Drafts" or published? If they are drafts, it is normal to be able to see them only if you are logged in, because a draft is not published so not supposed to be seen by any visitor. That's my guess. Otherwise, please give more info.
Ok, so I have the answer for you: index.php is not the file used to display http://www.ilovepennycakes.com/category/blog/. The file used for this type of archive display is "category.php". If there is no such file in your theme folder, add it (possibly duplicate index.php as you have programmed it or take it from a default example template).
I have a page with many items pulled from posts.
I have it set up to only display 10 posts at a time, but my previous/next button isn't actually displaying the next or previous posts - it justs keeps displaying the same posts.
Here's the function I wrote:
function add_shop() {
if (is_page('shop') || is_page('7')) { ?>
<div id="content">
<div class="post_box">
<div id="column_1">
<div id="shop_wrapper">
<?php query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10');
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="shop_item"> <img src="<?php getCustomField('shop_thumbnail_image'); ?>" alt='photo of <?php getCustomField('title'); ?>' class="shop_thumb" />
<div class="shop_content">
<h4><a href="<?php getCustomField('link_to_project_page'); ?>">
<?php getCustomField('title'); ?>
</a></h4>
<?php getCustomField('duration'); ?>
<?php getCustomField('paypal_code'); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php posts_nav_link(); ?>
</div>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
</div>
</div>
<div id="sidebars">
<div id="sidebar_1" class="sidebar">
<ul class="sidebar_list">
<li class="widget">
<div class="widget_box">
<?php dynamic_sidebar(5); ?>
</div>
</li>
</ul>
</div>
</div>
<?php } }
Based on the above help, I just just got it working with this:
<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($page == 1) {
query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10&paged=$page');
} else {
$numposts = get_option('posts_per_page');
// work out offset
$offset = (($page -1) * $numposts); // i.e. page 2 - 1 = 1 * 10 (10 for the number of posts)
query_posts("tag=shop&orderby=title&order=ASC&offset=$offset&paged=$page&showposts=$numposts");
}
if (have_posts()) : ?>
You're manually calling query_posts(), which will overwrite any variables related to fetching posts that wordpress tries to send itself. If you want to keep the query string that it's already sending, you should concatenate to it instead of replacing it:
query_posts($query_string.'&tag=shop&orderby=title&order=ASC&posts_per_page=10');
Alternatively, if you only want to include the "page" variable, include it with $paged:
query_posts('tag=shop&orderby=title&order=ASC&posts_per_page=10&paged='.$paged);