I did find one topic that sort of touched base on this but if there is a better link please advise.
I have a self hosted Wordpress website set up typically for a static home page and a separate page that shows recent posts called "Reviews". I need to write posts under a category "Recent News" that would be excluded from the recent post and shown on their own page, a second blog page.
The Codex tells me to add this to the index.php to exclude the category "recent-news":
<?php
if (is_home()) {
query_posts("cat=-3");
}
?>
with 3 being "recent-news". This does nothing no matter where I place it on the page. It still shows up with the recent posts. I'm guessing I'm in the wrong section for excluding categories in the Codex.
How do I exclude a category from recent posts, add a new page called "Recent News" and have it only show posts from the 'recent-news" category.
or have I climbed down a rabbit hole...
you can do it like this: edit your index.php page or create a custom template and then create a page (say "Front Page" ) and assign this template to it. In this template, copy your index.php code or if you feel confident simply create your template and include this code:
<?php if (have_posts()) : ?>
<?php $my_query=new WP_Query( 'cat=-3'); while
($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
<div class="post">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile;?>
<?php endif;?>
About your second question, simply go to Appearance --> Menu and there you'll see you can add pages but also categories. Simply select the category "Recent News" as a menu element and voilá, now you'll have a page displaying only posts from that category
I do not recommand messing with index.php
I think you should create a page template that will query posts from "Recent News" category.
Then in wordpress create a page and choose the page template you created.
In order to set it as the home page you will have to set it via wordpress configurations.
Wordpress page template
query posts by category
Set static homepage
Related
I've created my own WordPress custom theme for a website that I'm working on. It's my first time building a theme from scratch! I'm having a difficulty 1st: to link several pages to the fron-page, or home page. For example lets say that I want to link "Who we are", page + "Contact us", page, etc.. How can I make WordPress understands that each page is different, and link to it? here is what I did so far:
in page.php I placed this code:
<?php get_header(); ?>
<div class="container">
<?php
if (have_post()) {
while(have_post()) {
the_post();
get_template_part( 'template-parts/content', 'page');
}}
?>
</div>
<?php get_footer(); ?>
I also created a content-page.php where I've placed my HTML code. (I'm building the whole thing with an HTML and CSS code that's just like building a website from scratch).
I also want to know when using a plugin, how can I connect that into the pages that I'm adding. For example, if I'm installing a form such as WpForms for a page like "Contact us" how can I link that?
Please let me know if my questions need more details
If I understand this correctly...
You want to be able to display (link) a couple of pages to your home page?
Ex:
[home page content here]
[Connect page content here] [Blurb page here]
If so, you need to pull in the page content into the home page.
Something like:
<?php
$postid = 0 /*Need to get page id, that's the key */;
//Pull in the page data
$featured_page = get_post($postid);
$thumbnail = get_the_post_thumbnail( $featured_page->ID );
//Only show the excerpt in this case
$content = apply_filters( 'the_content', $featured_page->post_excerpt );
?>
<article id="post-<?php echo $featured_page->ID ?>">
<header class="entry-header">
<h1><?php echo $featured_page->post_title ?></h1>
</header>
<div class="entry-content">
<div class="thumbnail">
<?php echo $thumbnail; ?>
</div>
<div>
<?php echo $content; ?>
</div>
</div>
Important! You'll need to get the page id from somewhere. You can hard-code it in, but that's risky if something changes. You can set up an Customizer option that lets the user select what page to use.
https://codex.wordpress.org/Theme_Customization_API
As for the contact form etc. I'd suggest putting it on a page as a shortcode, then displaying the full page, not the excerpt.
I use this code for a theme I built myself. If you need anymore help, let me know, I can show you where to find it.
I have this theme on my ecommerce website and its called Soma. He is great but sadly, the team doesn't support anymore.
The question I have is how can I set up a category as a page? Cause on this particularly theme, the sidebar menu doesn't show up on normal pages, just on shop pages and categories pages.
I need to create a page that shows only one type of product, that's ease with woocommerce code. But the problem is that on that page, it doesn't show the sidebar menu.
I will put some prints here, maybe it can help.
Print of the problem:
<?php
/*
Default Template Page
*/
get_header();
if (have_posts()) : while (have_posts()) : the_post();
$soma_vc = preg_match("/\[vc_row.*?\]/i", get_the_content());
$soma_page_class = "container";
if ($soma_vc && function_exists('vc_map')) {
$soma_page_class = "n-container";
}
?>
<div class="<?php echo esc_attr($soma_page_class) ?>">
<?php
the_content();
// WP Link Pages
$args = array(
'before' => '<div class="navigation">',
'after' => '</div>'
);
wp_link_pages($args);
?>
</div>
<?php
endwhile; endif;
// Comments
comments_template();
get_footer();
And this is what I need to put there:
<?php if($soma_shop_sidebar != '2' && is_active_sidebar('shop-sidebar')) : ?>
<div class="soma-overlay" id="woo-sidebar_overlay"></div>
<div class="woo-sidebar fixed-lateral left item-delay_off">
<div class="hide-scrollbar">
<div class="inner-hide_scrollbar">
<div class="woo-sidebar_padding">
<?php echo esc_html__('Close', 'soma') ?>
<?php get_sidebar('shop'); ?>
</div>
</div>
</div>
</div>
This is one is still tough to assist on without seeing the full source of Soma, which isn't open-source.
However, if you simply need to customize the template used to display a single category, and there isn't a category.php template in the theme yet, this is the procedure:
1. Create a custom template for single categories
Referring to the WordPress Template Hierarchy, you'll want to create a custom template called category.php
If you want the layout for this page to be similar to another existing template, simply copy that template's code into category.php to begin.
2. Copy relevant sidebar code into your new category.php template
Add the sidebar code you've posted into your new category.php template, wherever it belongs in the layout.
If you're not sure where to put it, or you'd like to include a sidebar template in the same way that the Shop and Categories pages do:
look at code of those templates where the sidebar does currently appear
find the place where they include the sidebar template
do the same thing in your new category.php file
So, i finally got my css and js scripts to load on WP
Now there is but one thing i need to get done.
i have my own theme, including header.php, footer.php, page.php
header.php and footer.php is working just fine, loading scripts and showing properly, but now i need to add all the other content.
my page.php is currently:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
I would need to somehow add html content to pages, i have about 20 ready made .php pages which needs to be transfered to WP.
Soooo how do i go about making new page (pages -> add new) and using page template while getting the html content to show up?
I've tried to just make new page and in text mode add up all the html into page, but it only shows empty page with header and footer, so the problem is most likely the page.php and i have no idea how to get it to work.
You can do look like this:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php get_footer(); ?>
You are on the good way. While developing a custom theme from scratch is a great challenge it's not too hard.
I could recommend to take it easy and follow this tutorial I found really helpful some time ago, I learned a lot there:
Developing a WordPress Theme from Scratch
You must have the official source documentation always in your mind:
Theme Development
Do some reading and you will see that making themes is really fun and gratifying :)
EDIT:
I would recommend picking a good starter theme or framework and work using child themes. You have plenty of them to pick.
To get started adding a new page to your WordPress site, find the Pages menu in the WordPress Dashboard Navigation menu. Click Add new.
The WordPress page editor looks nearly identical to the post editor, except for a few different boxes located on the right side of the screen.
Add the title of the page, like About. Note: If you have pretty permalinks set up, the title of your page will also be the URL slug.
Next, add some content.
The Publish section of the page editor is exactly the same as for writing posts. When you’re ready to publish, you can either publish immediately, save this or a draft, or schedule the page to be published later.
The Page Attributes section applies a parent page and template to your new page. For the Parent section, you can arrange your pages into hierarchies. For example, you could create this new page with additional pages under it. There are no limits to how many levels you can nest pages.
Some WordPress themes have custom page templates, so the next Template section allows you to apply a template to your new page.
The Order box allows you to order your page numerically. Pages are usually ordered alphabetically, but you can choose your own order by entering a number in this field.
Preview the page one last time, then click Publish. You’ve added a new page to your WordPress site.
This is how your
your index.php should look like :
<?php
get_header();?>
<div class="YourContainer">
<div class="Whatever">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ContentSectionDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
<?php get_footer();?>
you can make also a custom loop
<?php
$arg = array("post_type" => "services",
"posts_per_page" => 9,
"order_by" => "date",
"order" => "ASC",
);
$services = new WP_Query($arg);
if ($services->have_posts()):;
while ($services->have_posts()):$services->the_post();
the_content();
endwhile;
endif;
?>
I have a theme that I am developing using the _s (aka: underscores) theme.
I do not want my archives to include the sidebar.php file.
My archive.php and paged.php files do not have the sidebar.php
included.
I created a home.php file which includes the get_sidebar()
function, and edited my index.php to not include that function.
I'm still seeing my sidebar when I click "Older Posts" on my site which brings me to: MYDOMAIN/?paged=2 -- this is not what I want.
Specific archive pages (such as for months and categories) do not display the sidebar, which is what I want.
I only want the sidebar to appear on the front page of the site.
I checked the body tags on my home page and on the paged "Older Posts" archive pages to determine what template is being rendered.
Home page:
<body class="home blog">
paged "Older Posts" archive page:
<body class="home blog paged paged-2">
This leads me to believe they are both using the home.php template. How can I get those pages to use a different template?
What am I doing wrong?
A friend helped me resolve this issue and I thought I'd share our solution. In order to make sure the sidebar appeared only on the front page of the blog and not appear on the ?paged= pages, we added some conditional statements around the call to the sidebar on home.php:
<?php if ( !is_paged() ) { ?>
<?php get_sidebar(); ?>
<?php } ?>
Additionally, this code could be added to the sidebar to keep it from appearing on other pages such as category pages, single posts, etc. While this wasn't necessary since I had used specific templates for those pages that did not include the sidebar, here it is in case it's useful for others:
<?php if ( is_front_page() ) { ?>
<!-- ALL CODE FOR YOUR SIDEBAR HERE -->
<?php } ?>
I'm developing a Wordpress theme. One of the sections on the front page is titled News. In that section the user should be able to insert information himself (I'll make a separate file called news.php which will php included into the markup), but I also want the content to be automatically updated once the user makes a post.
For example, if the user writes a new post, I want the news section to be automatically updated to something like this:
A new post was written, find it here (where 'here' is a hyperlink pointing to the url of the post).
edit Let me try to give a little more details so it's clearer.
The news section will be an unordered list.
Thus:
<ul>
<li>
News item 1
</li>
<li>
News item 2
</li>
</ul>
I want the user to be able to add content to the news section, aka make new News Items, but using the visual editor of Wordpress such that the user doesn't have to understand the code and doesn't have to copy/paste the LIs.
Furthermore, whenever a new post is published, I want it to show up as:
"new post published, find it here" where 'here' is a hyperlink with linking to the post.
Is this possible to accomplish?
Thanks, Amit
make a Categorie, or a Custom Post Type called, "News" so when the user creates a new post, but wants it to be in the "News" section all he has to do is click on the Category "News." You could also create a custom post type, just for "News" and have custom taxonomies etc as well.
Then when you want to print the News post in your theme, write use the query post function and limit the loop to "category_name=news"
If i correctly understood, you need write something like this:
<?php
query_posts(array('posts_per_page' => 1));
the_post();
?>
A new post was written, find it here
Latest news list
<?php
query_posts(array('posts_per_page' => 6));
?>
<ul>
<?php $count=0; if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
</ul>